﻿541.反转字符串-II
void turn(char* s, int l, int r)
{
    while (l < r)
    {
        char temp = s[l];
        s[l] = s[r];
        s[r] = temp;
        l++;
        r--;
    }
}

char* reverseStr(char* s, int k) {
    int len = strlen(s), count = 0;
    while (1)
    {
        if (len >= 2 * k)
        {
            len -= 2 * k;
            turn(s, 2 * k * count, 2 * k * count + k - 1);
            count++;
        }
        else
        {
            if (len >= k)
            {
                turn(s, 2 * k * count, 2 * k * count + k - 1);
            }
            else
            {
                turn(s, 2 * k * count, strlen(s) - 1);
            }
            break;
        }
    }

    return s;
}

551.学生出勤记录-I
bool checkRecord(char* s) {
    int a = 0, l = 0;
    for (int i = 0; i < strlen(s); i++)
    {
        if (s[i] == 'A')
        {
            a++;
            l = 0;
        }
        else if (s[i] == 'L')
        {
            l++;
            if (l == 3)
            {
                return false;
            }
        }
        else
        {
            l = 0;
        }
    }
    if (a < 2)
    {
        return true;
    }
    else
    {
        return false;
    }
}

557.反转字符串中的单词-III
void turn(char* s, int l, int r)
{
    while (l < r)
    {
        char temp = s[l];
        s[l] = s[r];
        s[r] = temp;
        l++;
        r--;
    }
}
char* reverseWords(char* s) {
    int pos = 0;
    for (int i = 0; i <= strlen(s); i++)
    {
        if (s[i] == ' ' || s[i] == '\0')
        {
            turn(s, pos, i - 1);
            pos = i + 1;
        }
    }
    return s;
}

566.重塑矩阵
/**
 * Return an array of arrays of size *returnSize.
 * The sizes of the arrays are returned as *returnColumnSizes array.
 * Note: Both returned array and *columnSizes array must be malloced, assume caller calls free().
 */
    int** matrixReshape(int** mat, int matSize, int* matColSize, int r, int c, int* returnSize, int** returnColumnSizes) {
    if (r * c != matSize * (*matColSize))
    {
        *returnSize = matSize;
        *returnColumnSizes = matColSize;
        return mat;
    }

    int** ans = (int**)malloc(sizeof(int*) * r);
    *returnColumnSizes = malloc(sizeof(int) * r);
    *returnSize = 0;
    for (int i = 0; i < r; i++)
    {
        ans[(*returnSize)++] = (int*)malloc(sizeof(int) * c);
    }

    int o = 0, p = 0;
    for (int i = 0; i < r; i++)
    {
        (*returnColumnSizes)[i] = c;
        for (int j = 0; j < c; j++)
        {
            ans[i][j] = mat[o][p++];
            if (p == (*matColSize))
            {
                o++;
                p = 0;
            }

        }
    }
    return ans;
}

575.分糖果
int distributeCandies(int* candyType, int candyTypeSize) {
    int arr[2000001] = { 0 };
    int type = 0;

    for (int i = 0; i < candyTypeSize; i++)
    {
        arr[candyType[i] + 100000]++;
        if (arr[candyType[i] + 100000] == 1)
        {
            type++;
        }
    }

    if (type > (candyTypeSize / 2))
    {
        return candyTypeSize / 2;
    }
    else
    {
        return type;
    }
}